package com.cskaoyan.user.service.impl;

import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @auther 10-何兴绕
 * @Date 2022年10月25日 18:46
 */
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    UserConverterMapper converterMapper;

    @Override
    public UserLoginResponse login(UserLoginRequest request) {

        String password = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());

        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("username", request.getUserName());
        criteria.andEqualTo("password", password);

        List<Member> members = memberMapper.selectByExample(example);

        if (members.size() == 0) {
            return null;
        }

        Member member = members.get(0);

        UserLoginResponse userLoginResponse = converterMapper.converter(member);
        userLoginResponse.setToken(member.getIsVerified());

        return userLoginResponse;
    }
}
